package com.boot.message.netty;

import com.boot.message.netty.client.sender.NettyClientSender;
import com.boot.message.netty.server.receiver.NettyServerReceiver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description netty的启动与关闭配置
 * @date 2019/12/16 20:14
 **/

@Configuration
public class NettyConfig {

    @Autowired
    NettyServerReceiver receiver;

    @Autowired
    NettyClientSender sender;

    @EventListener(ContextRefreshedEvent.class)
    public void onContextRefreshedEvent(ContextRefreshedEvent e) throws Exception {
        receiver.start();
        //        sender.connect();
        //        if (sender instanceof StringSender) {
        //            sender.write("你好");
        //        } else if (sender instanceof JSONSender) {
        //            ((JSONSender) sender)
        //                    .write(new Email("1621856595@qq.com", "我的邮箱", ""));
        //        }
    }

    @EventListener(ContextClosedEvent.class)
    public void onContextStoppedEvent(ContextClosedEvent e) {
        //        sender.disconnect();
        receiver.stop();
    }

}
